package com.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ems.entity.MenuEntity;
import com.ems.mapper.MenuMapper;
import com.ems.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.ConstUtil;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.MenuVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-10-24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> implements IMenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<MenuVo> queryList ( MenuVo menuVo ) {
        menuVo.setPage ( ( menuVo.getPage () - 1 ) * menuVo.getLimit () );
        List<MenuVo> menuVos = menuMapper.queryList ( menuVo );
        return menuVos;
    }

    @Override
    public Integer queryCount ( MenuVo menuVo ) {
        Integer count = menuMapper.queryCount ( menuVo );
        return count;
    }

    /**
     * 查询所有菜单，形成树结构
     * @param menuVo
     * @return
     */
    @Override
    public List<MenuVo> getList (MenuVo menuVo) {
        //1. 查询一级封装为key
        List<MenuVo> menuEntities = menuMapper.getList ( menuVo );
        //2. 查询每个一级的所有二级
        for ( MenuVo menu : menuEntities ) {
            List<MenuEntity> childEntities = baseMapper.selectList ( new QueryWrapper<MenuEntity> ().eq ( "type", ConstUtil.MENU_TYPE_CHILD ).eq ( "parentId", menu.getId () ).orderByAsc ( "sort" ) );
            List<MenuVo> menuVos = new ArrayList<> ();
            for ( MenuEntity childEntity : childEntities ) {
                MenuVo vo = new MenuVo ();
                BeanUtils.copyProperties ( childEntity, vo );
                menuVos.add ( vo );
            }
            menu.setMenuEntities ( menuVos );
        }
        return menuEntities;
    }


    /**
     * 登录时获取左侧菜单，获取父子树形结构
     * @param menuVo
     * @return
     */
    @Override
    public List<MenuVo> queryMenus (MenuVo menuVo) {
        if ( menuVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        QueryWrapper<MenuEntity> queryWrapper = new QueryWrapper<> ();
        if ( menuVo.getType () != null ) {
            queryWrapper.eq ( "type", menuVo.getType () );
        }
        if ( StringUtils.isNotBlank ( menuVo.getParentId () ) ) {
            queryWrapper.eq ( "parentId", menuVo.getParentId () );
        }
        if ( StringUtils.isNotBlank ( menuVo.getRoleId () ) ) {
            queryWrapper.eq ( "roleId", menuVo.getRoleId () );
        }
        List<MenuVo> menuEntities = menuMapper.getList ( menuVo );
        for ( MenuVo menu : menuEntities ) {
            MenuVo child = new MenuVo ();
            child.setParentId ( menu.getId () );
            child.setType ( ConstUtil.MENU_TYPE_CHILD );
            child.setRoleId ( menuVo.getRoleId () );
            List<MenuVo> childMenus = menuMapper.getList ( child );
            menu.setMenuEntities ( childMenus );
        }
        return menuEntities;
    }

    /**
     * 新增菜单
     * @param menuVo
     * @return
     */
    @Override
    public MenuVo saveSingle (MenuVo menuVo) {
        // 获取所有菜单
        Integer count = baseMapper.selectCount ( new QueryWrapper<> () );
        MenuEntity menuEntity = new MenuEntity ();
        BeanUtils.copyProperties ( menuVo, menuEntity );
        menuEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        menuEntity.setSort ( count + 1 );
        menuEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( menuEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return menuVo;
    }

}
